/*
 * Copyright 2022 Chaos Mesh Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
import fs from 'fs'
import { generate as orval } from 'orval'
import { rimraf } from 'rimraf'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'

import { appPath } from './constants.js'
import { genForms } from './index.js'

const argv = yargs(hideBin(process.argv))
  .command('all', 'generate API client and Formik form data')
  .command('client', 'generate API client by orval')
  .command('formik', 'convert the definitions generated by orval to Formik form data')
  .alias('help', 'h')
  .version(false)
  .wrap(120).argv

// eslint-disable-next-line default-case
switch (argv._[0]) {
  case 'all':
    await runClient()
    runFormik()

    break
  case 'client':
    runClient()

    break
  case 'formik':
    runFormik()

    break
}

async function runClient() {
  fs.copyFileSync('../../../pkg/dashboard/swaggerdocs/swagger.yaml', './swagger.yaml')
  const swaggerFile = fs.readFileSync('./swagger.yaml', 'utf8')
  // Remove module prefix from the swagger file.
  fs.writeFileSync('./swagger.yaml', swaggerFile.replace(/github_com_chaos-mesh_chaos-mesh.*_/g, ''))

  await callOrval()
  rimraf('./swagger.yaml')
}

function callOrval() {
  return orval('./orval.config.ts')
}

function runFormik() {
  genForms(`${appPath}/src/openapi/index.schemas.ts`)
}
